#include "GameplayUI.h"
#include "s3e.h"

GameplayUI::GameplayUI()
{ }

void GameplayUI::Load()
{
	m_bg.Load( "ingame_ui_bg" );
	m_digits.Load( "digit_font", 13 ); 
	m_pause.Load( "pause_button" );
	
	m_pause_button.CalcSizeForSprite( m_pause );
	
	// and set the position
	m_pause_button.SetPos( CIwFVec2(480 - 30, 0) );
}

void GameplayUI::Unload()
{
	m_bg.Unload();
	m_digits.Unload(); 
	m_pause.Unload();
}

void draw_int( Sprite& font, int number, CIwSVec2 start_pos )
{
	int digitBuff[10];

	int min_digits = 1;
	
    int index = 0;
    while (number > 0 || index < min_digits) {
        digitBuff[index] = number % 10;
        number /= 10;
        ++index;
    }
    
    // now draw the score
    for( int i = 0; i < index; ++i ) {
		font.DrawFrame( start_pos, digitBuff[i] );
		
		start_pos.x -= font.GetFrameSize().x;
    }
}

void GameplayUI::Draw(int score, int highscore)
{
	CIwSVec2 bg_pos( 480 - 172, 0);
	m_bg.Draw( bg_pos );
	
	CIwSVec2 score_offset( 480 - 100, 0 );
	draw_int( m_digits, score, score_offset );
	
	CIwSVec2 highscore_offset( 480 - 50, 0 );
	draw_int( m_digits, highscore, highscore_offset );
	
	// draw the pause button
	CIwSVec2 pause_pos( 480 - 30, 0 );
	m_pause.Draw( pause_pos );
}

bool GameplayUI::Update(float dt)
{
	if( m_pause_button.IsWithin( 0 ) && s3ePointerGetTouchState( 0 ) & S3E_POINTER_STATE_RELEASED ) {
		return true;
	}

	return false; 
}